#include"Sqlist.h"
void SLInit(SL* ps)
{
	assert(ps);
	ps->arr = NULL;
	ps->capacity = ps->size = 0;
}
void SLDestroy(SL* ps)
{
	assert(ps);
	free(ps->arr);
	ps->capacity = ps->size = 0;
}
void SLPushCheck(SL* ps)
{
	if (ps->capacity == ps->size)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, newcapacity * sizeof(SLDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			return;
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
}
void SLPushBack(SL* ps, SLDataType x)
{
	assert(ps);
	SLPushCheck(ps);
	ps->arr[ps->size++] = x;
}
void SLPrint(SL s)
{
	for (int i = 0; i < s.size; i++)
	{
		printf(" %d", s.arr[i]);
	}
	printf("\n");
}
void SLPushFront(SL* ps, SLDataType x)
{
	assert(ps);
	SLPushCheck(ps);
	for (int i = ps->size - 1; i >= 0 ; i--)
	{
		ps->arr[i + 1] = ps->arr[i];
	}
	ps->arr[0] = x;
	ps->size++;
}
void SLPopBack(SL* ps, SLDataType x)
{
	assert(ps);
	assert(ps->size);
	ps->size--;
}
void SLPopFront(SL* ps)
{
	assert(ps);
	assert(ps->size);
	for (int i = 0; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
void SLPushPos(SL* ps, int pos, SLDataType x)
{
	assert(ps);
	assert(pos >= 0 && pos <= ps->size);
	SLPushCheck(ps);
	for (int i = ps->size; i > pos; i--)
	{
		ps->arr[i] = ps->arr[i-1];
	}
	ps->arr[pos] = x;
	ps->size++;
}
void SLErasePos(SL* ps, int pos)
{
	assert(ps);
	assert(pos >= 0 && pos < ps->size);
	for (int i = pos; i < ps->size-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}
int SLFind(SL ps, SLDataType x)
{
	assert(ps.size);
	for (int i = 0; i < ps.size; i++)
	{
		if (ps.arr[i] == x)
		{
			return i;
		}
	}
	return -1;
}